{% extends 'mobile_app/base.html' %}
{% load static %}

{% block title %}注册 - 郑州移不动{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-md-6 col-lg-5">
        <div class="card border-0 shadow">
            <div class="card-body p-4">
                <div class="text-center mb-4">
                    <i class="fas fa-user-plus text-primary mb-3" style="font-size: 3rem;"></i>
                    <h3 class="fw-bold">注册账户</h3>
                    <p class="text-muted">创建您的郑州移不动账户</p>
                </div>

                <form method="post">
                    {% csrf_token %}
                    
                    <div class="mb-3">
                        <label for="{{ form.username.id_for_label }}" class="form-label">手机号码</label>
                        <div class="input-group">
                            <span class="input-group-text">
                                <i class="fas fa-phone"></i>
                            </span>
                            {{ form.username }}
                        </div>
                        {% if form.username.errors %}
                            <div class="text-danger small mt-1">
                                {% for error in form.username.errors %}
                                    <div>{{ error }}</div>
                                {% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">用户名将作为您的主手机号码</div>
                    </div>

                    <div class="mb-3">
                        <label for="{{ form.email.id_for_label }}" class="form-label">邮箱地址</label>
                        <div class="input-group">
                            <span class="input-group-text">
                                <i class="fas fa-envelope"></i>
                            </span>
                            {{ form.email }}
                        </div>
                        {% if form.email.errors %}
                            <div class="text-danger small mt-1">
                                {% for error in form.email.errors %}
                                    <div>{{ error }}</div>
                                {% endfor %}
                            </div>
                        {% endif %}
                    </div>

                    <div class="mb-3">
                        <label for="{{ form.password1.id_for_label }}" class="form-label">密码</label>
                        <div class="input-group">
                            <span class="input-group-text">
                                <i class="fas fa-lock"></i>
                            </span>
                            {{ form.password1 }}
                        </div>
                        {% if form.password1.errors %}
                            <div class="text-danger small mt-1">
                                {% for error in form.password1.errors %}
                                    <div>{{ error }}</div>
                                {% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">至少8个字符，不能全是数字</div>
                    </div>

                    <div class="mb-4">
                        <label for="{{ form.password2.id_for_label }}" class="form-label">确认密码</label>
                        <div class="input-group">
                            <span class="input-group-text">
                                <i class="fas fa-lock"></i>
                            </span>
                            {{ form.password2 }}
                        </div>
                        {% if form.password2.errors %}
                            <div class="text-danger small mt-1">
                                {% for error in form.password2.errors %}
                                    <div>{{ error }}</div>
                                {% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">再次输入密码确认</div>
                    </div>

                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary btn-lg">
                            <i class="fas fa-user-plus me-2"></i>立即注册
                        </button>
                    </div>
                </form>

                <div class="text-center mt-4">
                    <p class="text-muted">
                        已有账户？ 
                        <a href="{% url 'mobile_app:login' %}" class="text-decoration-none">
                            立即登录
                        </a>
                    </p>
                </div>
            </div>
        </div>

        <!-- 注册福利 -->
        <div class="card border-0 bg-success text-white mt-3">
            <div class="card-body text-center">
                <h6 class="mb-2">
                    <i class="fas fa-gift me-2"></i>注册即送福利
                </h6>
                <p class="small mb-0">
                    新用户注册即送100元话费 + 5GB流量
                </p>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // 为表单字段添加Bootstrap样式
    $('input[type="text"], input[type="email"], input[type="password"]').addClass('form-control');
    
    // 手机号码格式验证
    $('#id_username').on('input', function() {
        var value = $(this).val();
        var phoneRegex = /^1[3-9]\d{9}$/;
        if (value.length === 11 && !phoneRegex.test(value)) {
            $(this).addClass('is-invalid');
        } else {
            $(this).removeClass('is-invalid');
        }
    });

    // 密码强度检查
    $('#id_password1').on('input', function() {
        var password = $(this).val();
        var strength = 0;
        
        if (password.length >= 8) strength++;
        if (/[a-z]/.test(password)) strength++;
        if (/[A-Z]/.test(password)) strength++;
        if (/[0-9]/.test(password)) strength++;
        if (/[^A-Za-z0-9]/.test(password)) strength++;
        
        $(this).removeClass('is-valid is-invalid');
        if (strength >= 3) {
            $(this).addClass('is-valid');
        } else if (password.length > 0) {
            $(this).addClass('is-invalid');
        }
    });

    // 确认密码验证
    $('#id_password2').on('input', function() {
        var password1 = $('#id_password1').val();
        var password2 = $(this).val();
        
        $(this).removeClass('is-valid is-invalid');
        if (password2.length > 0) {
            if (password1 === password2) {
                $(this).addClass('is-valid');
            } else {
                $(this).addClass('is-invalid');
            }
        }
    });
});
</script>
{% endblock %}
